package com.onix.scalecamerapreview.translate;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class LanguageSelectView extends View {
    private String[] words = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};
    private Paint paint;

    private int indexViewWidth;
    private int itemIndexViewHeight;

    private int heightMeasureSpec;

    private int itemHeight;


    public LanguageSelectView(Context context) {
        super(context);
    }

    public LanguageSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setTextSize(40);
        paint.setAntiAlias(true);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setColor(Color.WHITE);
    }


        private IndexBack iBack2;

        public void setBack(IndexBack iBack2) {
            this.iBack2 = iBack2;
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

            itemIndexViewHeight = heightMeasureSpec / words.length;
            indexViewWidth = widthMeasureSpec;

            this.heightMeasureSpec = heightMeasureSpec;

            // setMeasuredDimension(indexViewWidth, indexViewHeight);
            // itemHeight = getMeasuredHeight() / words.length;
            itemHeight = MeasureSpec.getSize(heightMeasureSpec) / words.length;

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            int w = MeasureSpec.getSize(indexViewWidth);
            int h = MeasureSpec.getSize(itemIndexViewHeight);
            int defaultH = MeasureSpec.getSize(heightMeasureSpec);

            int defaultH26 = defaultH / words.length;


            for (int i=0; i<words.length;i++) {
                if (wordIndexValue==i) {
                    paint.setColor(Color.BLACK);
                    paint.setTextSize(50);
                } else {
                    paint.setColor(Color.WHITE);
                    paint.setTextSize(40);
                }

                String word = words[i];

                // 我要得到字体到宽度
                Rect rect = new Rect();
                // 需要用到画笔 把值处理好传递给Rect
                paint.getTextBounds(word, 0, 1, rect);

                canvas.drawText(word, (w/2) - rect.width()/2, i==0?defaultH26:defaultH26*(i+1), paint);
            }

        }

        private int wordIndexValue = -1;

        private int tempWordIndexValue;

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:

                    float Y = event.getY();
                    wordIndexValue = (int) (Y/itemHeight);//字母索引

                    if (tempWordIndexValue != wordIndexValue) {
                        iBack2.back(wordIndexValue, words[wordIndexValue]);
                    }
                    tempWordIndexValue = wordIndexValue;
                    invalidate();

                    break;
                case MotionEvent.ACTION_UP:
                    iBack2.upEnd();

                    tempWordIndexValue = 0;

                    wordIndexValue = -1;
                    invalidate();
                    break;
                default:
                    break;
            }
            return true;
        }
        public interface IndexBack{
            void back(int index, String word);
            void upEnd();
        }
}
